import Mock from 'mockjs'
import codeMock from './codeMock/codeMock'
import zsp from './zsp/toMoreTable'
import xjy from './xjy/index'
import SysMock from './SysMock/mock'
import Manage from './mnn/manage'
import ProcessManage from './ProcessManage/ProcessManage'
Mock.setup({
    timeout: '200-600'
})

// 用户管理
let {classiFicationGoods, CommodityComptoents,list} = Mock.mock({
    'classiFicationGoods|10': [
        {
            "id|+1": 1,
            name: ('@pick(["电脑办公", "一级分类"])'),
            img: '/img/vol.png',
            ifTrue: ('@pick(["是", "否"])'),
            remark: '1',
            createUser: '超级管理员',
            date: '@datetime()'
        },
    ],
    'CommodityComptoents|20': [
        {
            "id|+1": 1,
            name: ('@last'),
            img: '',
            specification: '',
            price: /\d[1-9]\d[1-9]/,
            ifTrue: '@boolean',
            remark: '',
            createUser: ('@pick(["超级管理员", "测试管理员"])'),
            date: '@datetime()'
        }
    ],
    'list|50': [
        {
            'id|+1': 1,
            'time': '@datetime',
            'name': '@cname()',
            'phone|110000000110-19999999999': 1778855110,
            'province': '@province',
            'city': '@city',
            'county': '@county',
            'master': '@name',
            'address': '@province' + '@city' + '@county',
            'bz': ' '
        }
    ]
})

// 拦截get
Mock.mock('sg', 'get', (option) => {
    return list
})
// 拦截修改
Mock.mock('sg/change', 'post', (option) => {
    console.log(JSON.parse(option.body));
    list.forEach(r => {
        // console.log(option.body.id);
        if (JSON.parse(option.body).id == r.id) {
            r.time = JSON.parse(option.body).time,
                r.name = JSON.parse(option.body).name,
                r.phone = JSON.parse(option.body).phone,
                r.province = JSON.parse(option.body).province,
                r.city = JSON.parse(option.body).city,
                r.county = JSON.parse(option.body).county
            r.master = JSON.parse(option.body).master
            r.address = r.province + r.city + r.county
        }
    })
})
// 查询单一个接口
Mock.mock(/^sg\/find/, 'get', (option) => {
    let num = option.url.split('=')[1]
    // console.log(num,list.find(r=>r.id==num));
    return list.find(r => r.id == num)
})

// 查询分页接口
Mock.mock(/^sg\/Page/, 'get', (option) => {
    let obj = {}
    option.url.split('?')[1].split('&').forEach(r => {
        let a = r.split('=')
        obj[a[0]] = a[1]
    })
    let { currentPage4, pageSize4, search } = obj
    // console.log(+currentPage4,+pageSize4);
    let string1 = decodeURIComponent(search)
    if (string1 == '') {
        return {
            data: list.slice((currentPage4 - 1) * pageSize4, currentPage4 * pageSize4),
            count: list.length
        }
    } else {

        return {
            data: mohuchaxun1(string1, list).slice((currentPage4 - 1) * pageSize4, currentPage4 * pageSize4),
            count: mohuchaxun1(string1, list).length
        }
    }

})
// 拦截新增
Mock.mock('sg/push', 'post', (option) => {
    list.push(JSON.parse(option.body))
    console.log(JSON.parse(option.body));
})
// 拦截删除接口
Mock.mock('sg/delete', 'post', (option) => {
    // console.log(JSON.parse(option.body).id[0].id);
    JSON.parse(option.body).id.forEach(r => {
        console.log(r.id);
        list.splice(list.findIndex(e => e.id == r.id), 1)
        // console.log(list.findIndex(e=>e.id==r.id));
    })
    // list.splice(JSON.parse(option.body),1)
})







// classiFicationGoods接口
// 查询全部接口
Mock.mock('cxjGetAll', 'get', (option) => {
    return classiFicationGoods
})
// 根据id查询一个接口
Mock.mock(/^cxjGetOne/, 'get', (option) => {
    let num = option.url.split('=')[1]
    return classiFicationGoods.find(r => r.id == num)
})
// 分页查询
Mock.mock(/^cxjPage/, 'get', (option) => {
    let obj = {}
    // pageIndex PageSize 数量
    option.url.split('?')[1].split('&').map(r => {
        let b = r.split('=')
        obj[b[0]] = b[1]
    })
    let { pageIndex, pageSize, id, name, ifTrue, remark } = obj
    let string1 = decodeURIComponent(name)
    let string2 = decodeURIComponent(remark)
    let string3 = decodeURIComponent(ifTrue)
    let returnObj = classiFicationGoods
    switch (+(id != '')) {
        case 0:
            break;
        case 1:
            returnObj = returnObj.filter(r => r.id == id)
    }
    switch (+(string1 != '')) {
        case 0:
            break;
        case 1:
            returnObj = returnObj.filter(r => r.name == string1)
    }
    switch (+(string2 != '')) {
        case 0:
            break;
        case 1:
            returnObj = returnObj.filter(r => r.remark == string2)
    }
    switch (+(string3 != '')) {
        case 0:
            break;
        case 1:
            returnObj = returnObj.filter(r => r.ifTrue == string3)
    }

    return {
        data: returnObj.slice((pageIndex - 1) * pageSize, pageIndex * pageSize),
        count: returnObj.length
    }

})
// 添加接口
Mock.mock('cxjGetAdd', 'post', (option) => {
    let stu = JSON.parse(option.body)
    if (stu.id == '') return false
    if (classiFicationGoods.some(r => r.id == stu.id)) return false
    else {
        classiFicationGoods.push({
            id: stu.id,
            name: stu.name,
            img: '/img/vol.png',
            ifTrue: stu.ifTrue,
            remark: stu.remark,
            createUser: '超级管理员',
            date: stu.date
        })
        return true
    }

})
// 删除接口
Mock.mock('cxjdel', 'post', (option) => {
    classiFicationGoods.splice(classiFicationGoods.findIndex(r => r.id == JSON.parse(option.body).id), 1)
    return true
})

// 修改接口
Mock.mock('cxjupdate', 'post', (option) => {
    let stu = classiFicationGoods.find(r => r.id == JSON.parse(option.body).id)
    let obj = JSON.parse(option.body)
    stu.id = obj.id
    stu.name = obj.name
    stu.img = obj.img
    stu.ifTrue = obj.ifTrue
    stu.remark = obj.remark
    stu.createUser = obj.createUser
    stu.data = obj.data
    return true
})

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// CommodityComptoents接口
// 查询全部接口
Mock.mock('https://www.vol.com/CommodityComptoents/getAll', 'get', (option) => {
    return CommodityComptoents
})

// 改变状态接口
Mock.mock(/https:\/\/www.vol.com\/CommodityComptoents\/changeState/, 'get', (option) => {
    let obj = {}
    option.url.split('?')[1].split('&').map(r => {
        let b = r.split('=')
        obj[b[0]] = b[1]
    })
    CommodityComptoents[CommodityComptoents.findIndex(r => r.id == obj.id)].ifTrue = !CommodityComptoents[CommodityComptoents.findIndex(r => r.id == obj.id)].ifTrue
})

// 分页接口
Mock.mock(/https:\/\/www.vol.com\/CommodityComptoents\/pageAll/, 'get', (option) => {
    let obj = {}
    // pageIndex PageSize 数量
    option.url.split('?')[1].split('&').map(r => {
        let b = r.split('=')
        obj[b[0]] = b[1]
    })
    let { pageIndex, pageSize, name } = obj
    let string = decodeURIComponent(name)

    if (string == '' || string == 'undefined') {
        return {
            data: CommodityComptoents.slice((pageIndex - 1) * pageSize, pageIndex * pageSize),
            count: CommodityComptoents.length
        }
    } else {
        return {
            data: mohuchaxun1(string, CommodityComptoents).slice((pageIndex - 1) * pageSize, pageIndex * pageSize),
            count: mohuchaxun1(string, CommodityComptoents).length
        }
    }
})

// 添加接口
Mock.mock('https://www.vol.com/CommodityComptoents/add', 'post', (option) => {
    let stu = JSON.parse(option.body)
    stu.ifTrue == 'true' ? stu.ifTrue = true : stu.ifTrue = false
    CommodityComptoents.push(
        {
            id: CommodityComptoents.length + 1,
            name: stu.name,
            img: '',
            specification: stu.specification,
            price: stu.price,
            ifTrue: stu.ifTrue,
            remark: stu.remark,
            createUser: stu.createUser,
            date: NewDate()
        }
    )
    return true
})

// 删除接口
Mock.mock('https://www.vol.com/CommodityComptoents/del', 'post', (option) => {
    CommodityComptoents.splice(CommodityComptoents.findIndex(r => r.id == JSON.parse(option.body).id), 1)
    return true
})
// 查找单个接口
Mock.mock(/^https:\/\/www.vol.com\/CommodityComptoents\/GetId/, 'get', (option) => {
    let num = option.url.split('=')[1]
    return CommodityComptoents.find(r => r.id == num)
})

// 修改接口
Mock.mock('https://www.vol.com/CommodityComptoents/update', 'post', (option) => {
    let stu = CommodityComptoents.find(r => r.id == JSON.parse(option.body).id)
    let obj = JSON.parse(option.body)
    obj.ifTrue == 'true' ? obj.ifTrue = true : obj.ifTrue = false
    stu.id = obj.id
    stu.name = obj.name
    stu.img = obj.img
    stu.ifTrue = obj.ifTrue
    stu.remark = obj.remark
    stu.specification = obj.specification
    stu.price = obj.price
    return true
})

// 时间函数
let NewDate = () => {
    let date = new Date()
    let year = date.getFullYear()
    let month = date.getMonth() + 1
    month < 10 ? month = '0' + month : month
    let day = date.getDate()
    day < 10 ? day = '0' + day : day
    let hour = date.getHours()
    hour < 10 ? hour = '0' + hour : hour
    let minutes = date.getMinutes()
    minutes < 10 ? minutes = '0' + minutes : minutes
    let seconds = date.getSeconds()
    seconds < 10 ? seconds = '0' + seconds : seconds
    return year + "-" + month + '-' + day + ' ' + hour + ':' + minutes + ':' + seconds
}
// 模糊查询函数
function mohuchaxun1(name, object) {
    let obj = []
    let a = []
    object.forEach(r => {
        a.push(r.name)
    })
    let arr = [];
    for (var i = 0; i < a.length; i++) {
        if (a[i].indexOf(name) >= 0) {
            arr.push(a[i]);
        }
    }
    arr.forEach(r => {
        obj.push(object.find(s => s.name == r))
    })
    return obj
}
export default Mock